package perss.lance.jmsdemo;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

import javax.jms.*;

public class QueueConsumerTest {
    public static void main1(String[] args) throws JMSException {
        ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
        Destination queue = new ActiveMQQueue("lance.queue");
        Connection con = factory.createConnection();
        con.start();
        Session sen = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageConsumer consumer = sen.createConsumer(queue);
        TextMessage textMessage = (TextMessage) consumer.receive();
        System.out.println(textMessage.getText());
        consumer.close();
        con.close();
    }

    public static void main2(String[] args) throws Exception {
        ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
        Destination queue = new ActiveMQQueue("lance.queue");
        Connection con = factory.createConnection();
        con.start();
        Session sen = con.createSession(false, Session.CLIENT_ACKNOWLEDGE);
        MessageConsumer consumer = sen.createConsumer(queue);
        consumer.setMessageListener((arg0)->{
            TextMessage msg = (TextMessage) arg0;
            try {
                System.out.println(msg.getText());
            } catch (JMSException e) {
                e.printStackTrace();
            }
        });
        TextMessage textMessage = (TextMessage) consumer.receive();
        System.out.println("main thread here........");
        Thread.sleep(20000);
        con.close();
    }
}
